package com.jiawa.wiki.controller;

import com.jiawa.wiki.domain.Test;
import com.jiawa.wiki.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "测试controller")
public class TestController {
    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
    @Value("${test.hello:TEST}")
    private String testValue;

    @Autowired
    private TestService testService;

    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/test/hello")
    public String hello (){
        return "Hello World! "+testValue;
    }

    @ApiOperation("测试方法list")
    @GetMapping("/test/list")
    public List<Test> list (){
        return testService.list();
    }


    @ApiOperation("测试返回实体类")
    @PostMapping("/test/domain")
    public Test testDomain(Test test){
        return test;
    }



    /**
     * GET, POST, PUT, DELETE
     *
     * /user?id=1
     * /user/1
     * @return
     */
    // @PostMapping
    // @PutMapping
    // @DeleteMapping
    // @RequestMapping(value = "/user/1", method = RequestMethod.GET)
    // @RequestMapping(value = "/user/1", method = RequestMethod.DELETE)

    @ApiOperation("测试方法Post+带参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "测试参数名称", defaultValue = "描述值")
    })
    @PostMapping("/test/hello/post")
    public String helloPost(String name) {
        return "Hello World! Post，" + name;
    }



    @RequestMapping("/test/redis/set/{key}/{value}")
    public String set(@PathVariable Long key, @PathVariable String value) {
        redisTemplate.opsForValue().set(key, value, 3600, TimeUnit.SECONDS);

        LOG.info("key: {}, value: {}", key, value);
        return "success";
    }

    @RequestMapping("/redis/get/{key}")
    public Object get(@PathVariable Long key) {
        Object object = redisTemplate.opsForValue().get(key);
        LOG.info("key: {}, value: {}", key, object);
        return object;
    }
}
